﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class OrderManager_EditOrder : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            EntityOprHelper helper = new EntityOprHelper();
            DataTable _table = helper.GetDataTableBySql("SELECT NAME,FEE  FROM T_SENDMODE");

            ppsfs.DataValueField = "FEE";
            ppsfs.DataTextField = "NAME";

            if (_table != null)
            {
                ppsfs.DataSource = _table.DefaultView;
                ppsfs.DataBind();
            }
            RECORDID.Text = "";
            String rID = Page.Request["ID"];

            if (rID != null && rID != "")
            {
               
                T_ORDER add = helper.GetEntityByWhere<T_ORDER>(" where ID=" + rID + "");
                if (add != null)
                {
                    RECORDID.Text = rID;
                    pddh.Text = add.ORDERNO;
                    pyhmc.Text = add.UEERNAME;
                    pxddrqsj.Text = add.ORDERDATE;
                    pddzje.Text = add.ORDERTOTALPRICE.ToString("N");
                    pzszjfs.Text = add.INTEGRAL;
                    pff.Text = add.FEE.ToString("N");
                    pfklx.Text = add.PAYMODENAME;

                     int sfsIndex = 0;

                    int count = _table.Rows.Count;
                    for (int i = 0; i < count; i++)
                    {
                        if (_table.Rows[i][0].ToString() == add.SENDMODENAME)
                        {
                            sfsIndex = i;
                            break;
                        }
                    }

                    ppsfs.SelectedIndex = sfsIndex;

                    pddzt.Text = add.ORDERSTATE;
                    pqxddrq.Text = add.ORDERSTATE_CANCEL_DATETIME;
                    pqrddrq.Text = add.ORDERSTATE_OK_DATETIME;
                    pddfkrq.Text = add.ORDERSTATE_PAYMENT_DATETIME;
                    pfhrq.Text = add.ORDERSTATE_INVOICE_DATETIME;
                    pbz.Text = add.REMARK;

                    
                    String edit = Page.Request["act"];
                    if (edit != "Edit")
                    {
                        pzszjfs.Enabled = false;
                        ppsfs.Enabled = false;
                        pddzt.Enabled = false;
                        pbz.Enabled = false;
                    
                    }
                }
            }
        }
    }
    protected void Submit_Click(object sender, EventArgs e)
    {
        //保存信息
        EntityOprHelper helper = new EntityOprHelper();

        T_SMALLORDER _order = new T_SMALLORDER();
        _order.INTEGRAL =pzszjfs.Text;
        _order.SENDMODENAME = ppsfs.SelectedItem.Text;
        _order.ORDERSTATE = pddzt.Text;
        _order.REMARK = pbz.Text;
        _order.ID = Convert.ToInt32(RECORDID.Text);
        _order.FEE = Convert.ToDouble(pff.Text);

        if (pddzt.Text == "已付款")
        {
            _order.ORDERSTATE_PAYMENT_DATETIME = DateTime.Now.ToString();
        }
        if (pddzt.Text == "已确认")
        {
            _order.ORDERSTATE_OK_DATETIME = DateTime.Now.ToString();
        }
        if (pddzt.Text == "已发货")
        {
            _order.ORDERSTATE_INVOICE_DATETIME = DateTime.Now.ToString();
        }
       


        bool b = helper.EditEntity<T_SMALLORDER>(_order);
        if (b)
        {
            //如果保存成功，和状态为已付款，则累积积分
            if (pddzt.Text == "已付款")
            {
                string user=pyhmc.Text;
                double jfeng =Convert.ToDouble(pzszjfs.Text);

                T_USER _user=helper.GetEntityByWhere<T_USER>(" where NAME='" + user + "'");
                if (_user != null)
                {
                    _user.INTEGRAL = _user.INTEGRAL + jfeng;
                    helper.EditEntity<T_USER>(_user);
                }
            }
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('修改成功!')", true);
        }
        else
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('修改失败!')", true);
        }

    }
    protected void ppsfs_SelectedIndexChanged(object sender, EventArgs e)
    {
       double yfei= Convert.ToDouble(ppsfs.SelectedValue);
       pff.Text = yfei.ToString();


    }



}
